package com.orange.common.android.widget;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Vibrator;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.orange.score.app.ScoreApplication;
import com.orange.score.model.config.ConfigManager;
import com.orange.score.model.match.Match;
import com.orange.score.model.match.MatchManager;
import com.orange.score.model.match.ScoreUpdate;
import com.orange.score.service.LiveUpdateChangeCallBack;
import com.orange.score.service.MatchService;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class RealtimeScorePopupWindow
  implements LiveUpdateChangeCallBack
{
  private static TextView LeagueNameView;
  private static final String TAG = RealtimeScorePopupWindow.class.getName();
  private static TextView awayTeamNameView;
  private static TextView awayTeamScoreView;
  private static TextView homeTeamNameView;
  private static TextView homeTeamScoreView;
  private static MediaPlayer mp;
  private static PopupWindow popupWindow;
  private static TextView startTimeView;
  private static Timer timer = null;
  List<ScoreUpdate> scoreUpdateList = new ArrayList();

  static
  {
    mp = null;
  }

  public static void cancelDismissPopupWindowTimer()
  {
    if (timer != null)
    {
      timer.cancel();
      timer = null;
    }
    try
    {
      if (popupWindow != null)
      {
        popupWindow.dismiss();
        popupWindow = null;
      }
      return;
    }
    catch (Exception localException)
    {
    }
  }

  public static void setDismissPopupWindowTimer()
  {
    if (timer == null)
      timer = new Timer();
    timer.schedule(new TimerTask()
    {
      public void run()
      {
        try
        {
          if (RealtimeScorePopupWindow.access$1() != null)
          {
            Log.d(RealtimeScorePopupWindow.access$0(), "dismiss RealtimeScore PopupWindow...");
            RealtimeScorePopupWindow.access$1().dismiss();
            RealtimeScorePopupWindow.access$2(null);
          }
          return;
        }
        catch (Exception localException)
        {
          Log.e(RealtimeScorePopupWindow.access$0(), "dismiss RealtimeScore PopupWindow but catch exception", localException);
        }
      }
    }
    , 10000L);
  }

  public static void showPopupWindow(Activity paramActivity, ScoreUpdate paramScoreUpdate)
  {
    if (paramActivity == null)
      return;
    View localView1 = paramActivity.getWindow().getDecorView().findViewById(16908290);
    View localView2 = LayoutInflater.from(paramActivity).inflate(2130903093, null);
    if (popupWindow == null)
    {
      popupWindow = new PopupWindow(localView2, (int)TypedValue.applyDimension(1, 256.0F, paramActivity.getResources().getDisplayMetrics()), (int)TypedValue.applyDimension(1, 44.0F, paramActivity.getResources().getDisplayMetrics()));
      homeTeamNameView = (TextView)localView2.findViewById(2131558601);
      awayTeamNameView = (TextView)localView2.findViewById(2131558602);
      homeTeamScoreView = (TextView)localView2.findViewById(2131558603);
      awayTeamScoreView = (TextView)localView2.findViewById(2131558604);
      LeagueNameView = (TextView)localView2.findViewById(2131558599);
      startTimeView = (TextView)localView2.findViewById(2131558600);
    }
    String str1 = paramScoreUpdate.score;
    String str2 = str1.split(":")[0];
    String str3 = str1.split(":")[1];
    homeTeamNameView.setText(paramScoreUpdate.homeTeamName);
    awayTeamNameView.setText(paramScoreUpdate.awayTeamName);
    homeTeamScoreView.setText(str2);
    awayTeamScoreView.setText(str3);
    LeagueNameView.setText(paramScoreUpdate.leagueName);
    startTimeView.setText(paramScoreUpdate.minutes);
    homeTeamScoreView.setTextColor(Color.parseColor("#FF61180a"));
    awayTeamScoreView.setTextColor(Color.parseColor("#FF61180a"));
    if (paramScoreUpdate.homeAwayFlag == 1)
      homeTeamScoreView.setTextColor(-65536);
    try
    {
      while (true)
      {
        while (true)
        {
          popupWindow.dismiss();
          Log.d(TAG, "show RealtimeScore PopupWindow...");
          int i = (int)TypedValue.applyDimension(1, 60.0F, paramActivity.getResources().getDisplayMetrics());
          popupWindow.showAtLocation(localView1, 80, 0, i);
          boolean bool1 = ConfigManager.getInstance().isSound();
          boolean bool2 = ConfigManager.getInstance().isVibrate();
          if (bool1)
          {
            if (mp == null)
              mp = MediaPlayer.create(paramActivity, 2131099648);
            mp.start();
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
            {
              public void onCompletion()
              {
                paramMediaPlayer.stop();
                try
                {
                  paramMediaPlayer.prepare();
                  return;
                }
                catch (Exception localException)
                {
                  Log.e(RealtimeScorePopupWindow.access$0(), "erron on play goals sound.");
                }
              }
            });
          }
          if (bool2)
            ((Vibrator)paramActivity.getSystemService("vibrator")).vibrate(1000L);
          setDismissPopupWindowTimer();
        }
        if (paramScoreUpdate.homeAwayFlag != 2)
          break;
        awayTeamScoreView.setTextColor(-65536);
      }
      homeTeamScoreView.setTextColor(-65536);
      awayTeamScoreView.setTextColor(-65536);
    }
    catch (Exception localException)
    {
      while (true)
        Log.e(TAG, "dismiss popup window before show but catch exception ", localException);
    }
  }

  public void notifyScoreLiveUpdate(List<String[]> paramList)
  {
    int i;
    int k;
    this.scoreUpdateList.clear();
    if ((paramList == null) || (paramList.size() == 0));
    while (true)
    {
      do
      {
        return;
        i = 0;
        int j = paramList.size();
        k = 0;
        if (k < j)
          break label89;
      }
      while ((i == 0) || (this.scoreUpdateList.size() <= 0));
      showPopupWindow(ScoreApplication.currentActivity, (ScoreUpdate)this.scoreUpdateList.get(this.scoreUpdateList.size() - 1));
    }
    label89: String[] arrayOfString = (String[])paramList.get(k);
    if ((arrayOfString == null) || (arrayOfString.length == 0))
      Log.d(TAG, "notifyScoreLiveUpdate but data null or empty");
    while (true)
    {
      ScoreUpdate localScoreUpdate;
      do
      {
        Match localMatch;
        while (true)
        {
          while (true)
            ++k;
          localMatch = ScoreApplication.realtimeMatchService.getMatchManager().findMatchById(arrayOfString[0]);
          if (localMatch != null)
            break;
          Log.d(TAG, "notifyScoreLiveUpdate but match id not found, match id = " + arrayOfString[0]);
        }
        localScoreUpdate = ScoreUpdate.createScoreUpdateObject(localMatch, arrayOfString);
      }
      while ((localScoreUpdate == null) || (localScoreUpdate.updateType != 0));
      i = 1;
      this.scoreUpdateList.add(0, localScoreUpdate);
    }
  }
}